#pragma once
#include "..\duilib\UIlib.h"

using dui::UIPanel;
using dui::UIRenderManager;
using dui::CWindowWnd;
using dui::UINotification;
using dui::UIFrameBuilderDelegate;

// #include "xmldrv3/xmldrv.h"

typedef xmld::document xmlDocument;
typedef xmld::element xmlElement;
//////////////////////////////////////////////////////////////////////////
//CFormUI

class CWindowUI : public UIPanel
{
public:
    CWindowUI();
    virtual ~CWindowUI();

public:
    LPCTSTR GetClass() const;
    LPVOID GetInterface(LPCTSTR pstrName);

    SIZE GetInitSize();
    void SetInitSize(int cx, int cy);
    RECT& GetSizeBox();
    void SetSizeBox(RECT& rcSizeBox);
    RECT& GetCaptionRect();
    void SetCaptionRect(RECT& rcCaption);
    SIZE GetRoundCorner() const;
    void SetRoundCorner(int cx, int cy);
    SIZE GetMinInfo() const;
    SIZE GetMaxInfo() const;
    void SetMinInfo(int cx, int cy);
    void SetMaxInfo(int cx, int cy);
    bool IsShowUpdateRect() const;
    void SetShowUpdateRect(bool show);
    void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);

    void SetPos(RECT rc);

public:
    void			SetAlpha(int nOpacity);
    int			GetAlpha() const;

    void			SetBackgroundTransparent(bool bTrans);
    bool			GetBackgroundTransparent() const;

    void			SetDefaultFontColor(DWORD dwColor);
    DWORD	GetDefaultFontColor() const;

    void			SetDefaultSelectedFontColor(DWORD dwColor);
    DWORD	GetDefaultSelectedFontColor() const;

    void			SetDefaultDisabledFontColor(DWORD dwColor);
    DWORD	GetDefaultDisabledFontColor() const;

    void			SetDefaultLinkFontColor(DWORD dwColor);
    DWORD	GetDefaultLinkFontColor() const;

    void			SetDefaultLinkHoverFontColor(DWORD dwColor);
    DWORD	GetDefaultLinkHoverFontColor() const;

private:
    int			m_nOpacity;
    bool			m_bAlphaBackground;
    DWORD	m_dwDefaultFontColor;
    DWORD	m_dwDefaultDisabledFontColor;
    DWORD	m_dwDefaultLinkFontColor;
    DWORD	m_dwDefaultLinkHoverFontColor;
    DWORD	m_dwDefaultSelectedFontColor;
};

//////////////////////////////////////////////////////////////////////////
//CFormTestWnd

class CFormTestWnd : public CWindowWnd, public IUINotify
{
public:
    CFormTestWnd();
    ~CFormTestWnd();
    LPCTSTR GetWindowClassName() const;
    UINT GetClassStyle() const;
    void OnFinalMessage(HWND /*hWnd*/);

public:
    void SetManager(UIRenderManager* pPaintManager);
    UIRenderManager* GetManager() const;
    void SetRoot(UIWidget* pControl);
    void Init();

    void Notify(UINotification& msg);
    LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnNcPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnNcCalcSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnGetMinMaxInfo(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT OnKeyDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
    LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);

private:
    UIRenderManager* m_pManager;
    UIWidget* m_pRoot;
};

//////////////////////////////////////////////////////////////////////////
//CLayoutManager

class CLayoutManager : public UIFrameBuilderDelegate
{
public:
    CLayoutManager(void);
    virtual ~CLayoutManager(void);

public:
    void Init(HWND hWnd, LPCTSTR pstrLoad);

    void Draw(CDC* pDC);
    void DrawAuxBorder(CDC* pDC, UIWidget* pControl);
    void DrawGrid(CDC* pDC, CRect& rect);

    static UIWidget* NewUI(int nClass, CRect& rect, UIWidget* pParent, CLayoutManager* pLayout);
    static BOOL DeleteUI(UIWidget* pControl);
    static void ReleaseExtendedAttrib(UIWidget* pControl, UIRenderManager* pManager);
    UIRenderManager* GetManager();
    inline CWindowUI* GetForm() const;
    UIWidget* FindControl(CPoint point) const;

    void TestForm(LPCTSTR pstrFile);
    BOOL IsEmptyForm() const;
    void AlignLeft(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignRight(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignTop(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignBottom(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignCenterVertically(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignCenterHorizontally(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignHorizontal(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignVertical(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignSameWidth(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignSameHeight(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void AlignSameSize(UIWidget* pFocused, CArray<UIWidget*, UIWidget*>& arrSelected);
    void ShowGrid(bool bShow);
    bool IsShowGrid() const;
    void ShowAuxBorder(bool bShow);
    bool IsShowAuxBorder() const;
    void MicoMoveUp(CArray<UIWidget*, UIWidget*>& arrSelected, int nMoved);
    void MicoMoveDown(CArray<UIWidget*, UIWidget*>& arrSelected, int nMoved);
    void MicoMoveLeft(CArray<UIWidget*, UIWidget*>& arrSelected, int nMoved);
    void MicoMoveRight(CArray<UIWidget*, UIWidget*>& arrSelected, int nMoved);

    bool SaveSkinFile(LPCTSTR pstrPathName);
    static void SaveProperties(UIWidget* pControl, xmlElement& pParentNode
        , BOOL bSaveChildren = TRUE);

    void SetDefaultUIName(UIWidget* pControl);

    LPCTSTR GetSkinDir() const { return m_strSkinDir; }

    static UIWidget* CloneControls(UIWidget* pControl);
    static UIWidget* CloneControl(UIWidget* pControl);

    UIWidget* CreateWidget(LPCTSTR pstrClass);
protected:
    static void SaveControlProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveLabelProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveButtonProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveOptionProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveProgressProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveSliderProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveEditProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveScrollBarProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveListProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveComboProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveListHeaderProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveListHeaderItemProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveListElementProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveContainerProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveHorizontalLayoutProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveTileLayoutProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveActiveXProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveListContainerElementProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveItemProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveTabLayoutProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveChildWindowProperty(UIWidget* pControl, xmlElement& pNode);
    static void SaveWebBrowserProperty(UIWidget* pControl, xmlElement& pNode);

    static CString ConvertImageFileName(LPCTSTR pstrImageAttrib);

protected:
    class CDelayRepos
    {
    public:
        CDelayRepos();
        ~CDelayRepos();

    public:
        BOOL AddParent(UIWidget* pControl);
        void Repos();

    private:
        CArray<UIWidget*, UIWidget*> m_arrDelay;
    };

private:
    UIRenderManager m_Manager;
    CWindowUI* m_pFormUI;
    static CString m_strSkinDir;

    bool m_bShowGrid;
    bool m_bShowAuxBorder;
};

